<?php
	require('../database/bd_connect.php');
		
	if (isset($_GET['opt'])){	
		$cpe = $_GET['opt'] ;
	
		if (isset($_GET['query'])){	
			$query = $_GET['query'] ;

	$u_10_min;
	//$flag=0;
		//echo "1";
		if($query==1){
	$sql = "SELECT DATE_FORMAT(time, '%H:%i') as time FROM noise_data WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 12 MINUTE GROUP BY time";
	}
	else if($query==2){
	//ULTIMOS 16 HORAS INTERVALOS DE 2 HORAS
	$sql = "SELECT DATE_FORMAT(time, '%M %d') as time FROM noise_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 6 DAY group by day(time) order by date(time)";
	}
	else if($query==3){
	//ULTIMOS 7 DIAS INTERVALOS DE 12 HORAS
	$sql = "SELECT DATE_FORMAT(time, 'Week %U in %M') as time FROM noise_hour WHERE cpe_id=".$cpe." AND time >= NOW() - INTERVAL 3 week group by week(time) order by date(time)";
	}
	

	$result = $conn->query($sql);
	while ($row = mysqli_fetch_array($result)) {
			$time[] = $row['time'];
			//echo($row['noise']);
		}
	for($i=1;$i<=13;$i++){
			if($query==1){
	$sql = "SELECT abs(total_noise) AS noise FROM noise_data WHERE cpe_id=".$cpe." and channel=".$i." AND time >= NOW() - INTERVAL 10 MINUTE";
	}
	else if($query==2){
	$sql = "SELECT abs(average) AS noise FROM noise_hour WHERE cpe_id=".$cpe." and channel=".$i." AND time >= NOW() - INTERVAL 6 DAY group by day(time) order by date(time)";
	}
	else if($query==3){
	$sql = "SELECT abs(average) AS noise FROM noise_hour WHERE cpe_id=".$cpe." and channel=".$i." AND time >= NOW() - INTERVAL 3 week group by week(time) order by date(time)";
	}
		
	
		$result = $conn->query($sql);
		//$num_rows = mysqli_num_rows($result);
		
		while ($row = mysqli_fetch_array($result)) {
			$temp[] = $row['noise'];
			//echo($row['noise']);
		}
		$u_10_min[]= $temp; 
		$temp=null;
		
		
	}
	$u_10_min[] = $time; 
	$output = $u_10_min;
	//echo($output);
	echo json_encode($output);

	//echo('<br \>----fim');
}
}
else {
	echo "erro";
}
mysqli_close($conn);
?>



